#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

//#include <ncurses.h>
//#include <menu.h>


#include "read_fan_temperature.h"
#include "terminal_stdscr.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "../ttyrw_lib/ttyrw_lib.h"

#include "ec_it8733_rw.h"

#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#if 0
int const_nMaxColumn()
{
	return (const_nMaxCell*3-2);
}
#endif



extern EN_RUN_STATE_TYPE en_run_state;
void *thr_fn(void *arg);

static void disp_menu();

void disp_menu()
{
	dbg_rn_prt( "  3     read chip id" );
	dbg_rn_prt( "  4     start read cpu temperature and fan rpm" );
	dbg_rn_prt( "  5     start the thread read cpu temperature and fan rpm" );
	dbg_rn_prt( "  6     stop the thread read cpu temperature and fan rpm" );
	dbg_rn_prt( "  8     only once read cpu temperature and fan rpm" );
	dbg_rn_prt( "  9     exit this program" );
	dbg_rn_prt( "  u     the value of fan pwm +1" );
	dbg_rn_prt( "  d     the value of fan pwm -1" );

}

int ternimal_stdscr(int para_num)
{	int ch;

	disp_menu();

	while(en_run_state!=RUN_STATE_EXIT)
    {
		ch = getchar();			/* If raw() hadn't been called
						 * we have to press enter before it
						 * gets to the program 		*/
		//if(ch == defKeyFMenuBgn+6 )		/* Without keypad enabled this will */
		//	printw("F7 Key pressed");/*  not get to us either	*/
						/* Without noecho() some ugly escape
						 * charachters might have been printed
						 * on screen			*/

		dbg_rn_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );

		//if(ch >= defKeyFMenuBgn  && ch <= defKeyFMenuBgn+5 )
		{
			if(ch == '3')
			{
				unsigned char chipid0;
				unsigned char chipid1;
				CEcIt8733Rw::getInstance()->get_chip_id(&chipid0, &chipid1);
				dbg_rn_prt( "chipid0 =%02X; chipid1 =%02X",chipid0, chipid1 );

			}
			else if (ch == '4')
			{
				unsigned char u8FanPwmLocal;
				int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
				int nCpuFan;
				CEcIt8733Rw::getInstance()->ec_it8733_start_read_cpu_temp( &u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );

			}

			else if (ch == '5')
			{
				#if 0
				unsigned char u8FanPwmLocal;
				int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
				int nCpuFan;
				ec_it8733_read_cpu_temp( u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );
				#endif

				#if 1
				if(en_run_state!=RUN_STATE_START)
				{
					void entry_test_status(void);
					//entry_test_status();
					int err;
					pthread_t ntid;
					err = pthread_create(&ntid, NULL, thr_fn, NULL);
					if(err != 0)
					{
						en_run_state=RUN_STATE_EXIT;
						dbg_rn_prt( "can't create thread: %s ", strerror(err) );
					}
					else
					{
						en_run_state=RUN_STATE_START;
						dbg_rn_prt( "en_run_state: %4d ", en_run_state );
					}
				}
				#endif

			}
			
			else if (ch == '6')
			{
				en_run_state=RUN_STATE_STOP;
				dbg_rn_prt( "en_run_state: %4d ", en_run_state );
			}			

			else if (ch == 'u' || ch == 'U')
			{
				unsigned char u8FanPwm;
				CEcIt8733Rw::getInstance()->ec_it8733_get_fan_pwm(&u8FanPwm );

				u8FanPwm++;
				//if(u8FanPwm>)

				CEcIt8733Rw::getInstance()->ec_it8733_set_fan_pwm(u8FanPwm );

				dbg_rn_prt( "en_run_state: %4d ;u8FanPwm =%10d", en_run_state , u8FanPwm );
			}		

			else if (ch == 'd'|| ch == 'D')
			{
				unsigned char u8FanPwm;
				CEcIt8733Rw::getInstance()->ec_it8733_get_fan_pwm(&u8FanPwm );

				u8FanPwm--;
				//if(u8FanPwm>)

				CEcIt8733Rw::getInstance()->ec_it8733_set_fan_pwm(u8FanPwm );

				dbg_rn_prt( "en_run_state: %4d ;u8FanPwm =%10d", en_run_state , u8FanPwm );
			}			


			else if (ch == '8')
			{
				unsigned char u8FanPwmLocal;
							int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
				int nCpuFan;
				CEcIt8733Rw::getInstance()->ec_it8733_read_cpu_temp( &u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );

				dbg_rn_prt( "en_run_state: %4d ;u8FanPwm =%10d", en_run_state , u8FanPwmLocal );
			}		

			else if (ch == '9')
			{

				en_run_state=RUN_STATE_EXIT;
				dbg_rn_prt( "en_run_state: %4d ", en_run_state );
			}

			else if (ch == 10)  //enter
			{
				usleep(100*1000);
				disp_menu();

			}
			 

		}
	}

	return 0;
}



int tty_main(void)
{
 
	#define buffLen 1024
	int rcvTimeOutMsec = 10;

	CTtyRw* pObjTtyRw = CTtyRw::getInstance();
 
 	if(pObjTtyRw->openTty( (char*)"/dev/ttyS1"  ) <0)
	{
		delete pObjTtyRw;
		return -1;
	}
	 	
 
	char buffRcvData[buffLen] = { 0 };
	unsigned int readDataNum = 0;
 
	buffRcvData[0] = 's';
	buffRcvData[1] = 't';
	buffRcvData[2] = 'a';
	buffRcvData[3] = 'r';
	buffRcvData[4] = 't';
	
	pObjTtyRw->sendDataTty( buffRcvData, 5);

	
	for(int i=0;i<5;i++)
	{
		usleep(1000*100);
		memset(buffRcvData,0x00,sizeof(buffRcvData));
		readDataNum = pObjTtyRw->readDataTty( buffRcvData, rcvTimeOutMsec, buffLen);
		pObjTtyRw->sendDataTty( buffRcvData, 5);
	}
	sleep(1);

	delete pObjTtyRw; 
	sleep(1);
	
	
	return 1;
}


int tty_rw_test(char* strsend, int nlensend, char* strrecv, int *nlenresv)
{
 
	int rcvTimeOutMsec = 10;

	CTtyRw* pObjTtyRw = CTtyRw::getInstance();
 
 	if(pObjTtyRw->openTty( (char*)"/dev/ttyS1"  ) <0)
	{
		delete pObjTtyRw;
		return -1;
	}
	 	
	pObjTtyRw->sendDataTty( strsend, nlensend);

	usleep(1000*100);
	*nlenresv = pObjTtyRw->readDataTty( strrecv, rcvTimeOutMsec, buffLen);
	
	usleep(100*1000);
	delete pObjTtyRw; 
	usleep(100*1000);
	
	return 0;
}


